<template>
  <!-- 战败原因 -->
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="4"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn='false'
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" v-if="dataType !== '10461001'">
        <el-button size="small" type="primary" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <DefeatreasonModel :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="visibleDLR"
      :title="dialogTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <DefeatreasonDLRModel :type="handleType" :rowData="formData" @close="closeHandlerDLR" />
    </BaseDialog>
  </div>
</template>

<script>
import { searchDefeatReason,searchDefeatReasonDLR } from '@/api/potenCus/basedata/defeatreason';
import { dictionary } from '@/utils/dictMixin';
import { getDataType } from '@/assets/js/auth';
import DefeatreasonModel from './defeatreasonModel';
import DefeatreasonDLRModel from './defeatreasonDLRModel';
export default {
  name: 'defeatreason',
  mixins: [dictionary],
  components: {
  DefeatreasonModel,
  DefeatreasonDLRModel
  },
  data() {
    return {
      dataType: getDataType('dataType'),
      topFilterList: this.createTopFilters(),
      fetchParams: {},
      visible: false,
      visibleDLR: false,
      dialogTitle: '',
      fetchApi: getDataType('dataType') != 10461001 ?searchDefeatReason : searchDefeatReasonDLR,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf:{ add: '新增', edit: '编辑', show: '显示' },
    };
  },
  created() {
    this.init()
    // this.isShowData()
  },
  methods: {
    init(){
      if(this.dataType == 10461001){
        this.columns.splice(4,0,{
          title: '数据来源',
            dataIndex: 'dataSources',
            sorter: true,
            filter: true,
            filterType: 'checkbox',
            filterItems: this.createDictList('1045'),
            render: props => {
              return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        })
      }
    },
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '战败原因',
          fieldName: 'drDesc',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        }
      ];
    },
    //打开新增窗口（依据OEM的数据）
    dlrAddHandle(row){
      this.handleType = 'add';
      this.formData = row;
      this.showHandlerDLR();
    },
    //直接修改
    dlrEditHandle(row){
      this.handleType = 'edit';
      this.formData = row;
      this.showHandlerDLR();
    },
    showHandlerDLR(){
      this.dialogTitle = `战败原因${this.conf['edit']}`;
      this.visibleDLR = true;
    },
    closeHandlerDLR(val, type) {
      this.visibleDLR = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            let oemEditButton,dlrEditButton;
            if(this.dataType == 10461001 && props.row.dataSources == 10451002){
              //1.经销商，其次为主机厂的数据
              dlrEditButton = (
                <el-button size="mini" type="text" disabled={ this.dataType == 10461002&& props.row.dataSources==10451002? true:false } onClick={() => this.dlrAddHandle(props.row)}>
                  编辑
                </el-button>
              );
            }else if(this.dataType == 10461001 && props.row.dataSources == 10451001){
              dlrEditButton = (
                <el-button size="mini" type="text" disabled={ this.dataType == 10461002&& props.row.dataSources==10451002? true:false } onClick={() => this.dlrEditHandle(props.row)}>
                  编辑
                </el-button>
              );
            }else{
              oemEditButton = (
                <el-button size="mini" type="text" disabled={ this.dataType == 10461001&& props.row.dataSources==10451002? true:false } onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
                );
            }
            return (
              <div>
                {oemEditButton}
                {dlrEditButton}
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '战败原因',
          dataIndex: 'drDesc',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '描述',
          dataIndex: 'basedata',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          initialValue: 10041001,
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1004')}</span>;
          }
        },
        {
          title: '是否审批',
          dataIndex: 'isApproval',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isApproval, '1004')}</span>;
          }
        },
      ];
    },
    // isShowData() {
    //   if(this.dataType != 10461001){
    //     isShowTop = false
    //   } else {
    //     // this.columns[4].hidden = false
    //   }
    // },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.dialogTitle = `战败原因${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
};
</script>

<style lang="less" scoped>
</style>
